package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCDemo11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        System.out.println("请输入您的新密码：");
        String newPassword = sc.next();
        System.out.println("请输入新昵称：");
        String newNickname = sc.next();

        try(Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement()) {
            String sql = "update userinfo\n" +
                    "set  password = ?, nickname = ?\n" +
                    "where username = ? and password = ?";

            PreparedStatement pr = connection.prepareStatement(sql);
            pr.setString(1,newPassword);
            pr.setString(2,newNickname);
            pr.setString(3,username);
            pr.setString(4,password);
            int num = pr.executeUpdate();
            System.out.println(num > 0 ? "修改成功！" : "修改失败！");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
